﻿@{
    Layout = "";
}
@using DormMS.Model.Enum;


<link rel="stylesheet" type="text/css" href="Content/css/DormMS/muban.css" />
<!--自定义-->
<!--<link rel="stylesheet" href="css/freight_template.css">-->
<link rel="stylesheet" href="Content/css/DormMS/freight_template.css">
<style>
    .lml_body2 .lml_red {
        font-size: 14px;
        position: relative;
    }

    .bg_cont .lml_tit {
        margin: 0;
    }

    .bg_cont .ycheckbox.on .fa:before {
        top: -7px;
    }

    .lml_button {
        width: auto;
    }

    .panel-heading_1 {
        position: relative;
    }

    .panel-heading .lmlfl {
        color: #656565;
        font-size: 16px;
        font-weight: bold;
        margin-left: 14px;
    }

    .panel-heading .lmlfr {
        font-size: 14px;
        color: #959595;
        position: absolute;
        right: 10px;
        top: 7px;
    }

    .panel-heading .lml_ico {
        font-size: 20px;
        padding: 0 10px;
    }

        .panel-heading .lml_ico .fa {
            cursor: pointer;
        }

        .panel-heading .lml_ico .ico_del:after {
            content: '\e401';
        }

        .panel-heading .lml_ico .ico_edit {
            margin-right: 7px;
        }

            .panel-heading .lml_ico .ico_edit:after {
                content: '\e400';
            }

    .panel-heading .lml_wz {
        margin-right: 7px;
    }

    .panel-heading .lml_date, .panel-heading .lml_time {
        margin-right: 12px;
    }

    .panel-body1 .tab_box {
        margin-bottom: 30px;
    }

        .panel-body1 .tab_box .lml_tab td, .panel-body1 .tab_box .lml_tab th {
            padding: 5px 0;
            height: auto;
        }
</style>


<div ng-controller="FreightManageController">
    <form name="formValidate" novalidate="" class="form-validate form-horizontal ng-pristine ng-valid ng-valid-required ng-valid-pattern">
        <div class="panel panel-default" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
            <div class="panel-body lml_body lml_body1">
                <div class="add_btn fa">新增</div>
                <table ng-table="tableParams" class="table table-striped table-bproducted table-hover">
                    <tbody>
                        <tr ng-repeat="InfoManage in $data">
                            <td>
                                <div class="panel panel-default" style="width:113%;">
                                    <div class="panel-heading panel-heading_1">
                                        <span class="lmlfl">{{TotalData-$index}}.{{InfoManage.TemplateName}}（{{InfoManage.IsUsing?'使用中':'未使用'}}）</span>
                                        <div class="lmlfr">
                                            <span class="lml_wz">最后编辑时间：</span>
                                            <span class="lml_date">{{InfoManage.EditTime}}</span>
                                            <span class="lml_ico"><i class="ico_edit fa" accesskey="{{InfoManage.Id}}"></i><i class="ico_del fa" ng-click="DelFreight(InfoManage.Id,InfoManage.IsUsing)"></i></span>
                                        </div>
                                    </div>
                                    <div class="panel-body panel-body1">
                                        <div class="tab_box ">
                                            <table border="0" cellspacing="0" cellpadding="0" width="100%" class="lml_tab lml_tab1" ng-if="InfoManage.FreightType==@(EnumFreightTemplateType.按重量.GetHashCode())">
                                                <tr>
                                                    <th width="38%">配送区域</th>
                                                    <th width="10%">首重(克)</th>
                                                    <th width="10%">首费(元)</th>
                                                    <th width="10%">续重(克)</th>
                                                    <th width="10%">续费(元)</th>
                                                    <th width="22%">指定条件包邮(选填)</th>
                                                </tr>
                                                <tr ng-repeat="detail in InfoManage.FreightDetail">
                                                    <td>
                                                        <span class="mr-sm label label-primary ng-binding" ng-repeat="province in detail.AreaNames" style="word-wrap:break-word; display:inline-block;">{{province}}</span>
                                                    </td>
                                                    <td>{{detail.FirstCount}}</td>
                                                    <td>{{detail.FirstMoney}}</td>
                                                    <td>{{detail.ContiCount}}</td>
                                                    <td>{{detail.ContiMoney}}</td>
                                                    <td>{{detail.FullCount>0?'重量'+detail.FullCount+'克（含）以内包邮':'无'}}</td>
                                                </tr>
                                            </table>
                                            <table border="0" cellspacing="0" cellpadding="0" width="100%" class="lml_tab lml_tab1" ng-if="InfoManage.FreightType==@(EnumFreightTemplateType.按地区.GetHashCode())">
                                                <tr>
                                                    <th width="50%">配送区域</th>
                                                    <th width="15%">运费(元)</th>
                                                    <th width="35%">指定条件包邮(选填)</th>
                                                </tr>
                                                <tr ng-repeat="detail in InfoManage.FreightDetail">
                                                    <td>
                                                        <span class="mr-sm label label-primary ng-binding" ng-repeat="province in detail.AreaNames" style="word-wrap:break-word; display:inline-block;">{{province}}</span>
                                                    </td>
                                                    <td>{{detail.FirstMoney}}</td>
                                                    <td>{{detail.FullCount>0?'满'+detail.FullCount+'元（含）包邮':'无'}}</td>
                                                </tr>
                                            </table>
                                            <table border="0" cellspacing="0" cellpadding="0" width="100%" class="lml_tab lml_tab2" ng-if="InfoManage.FreightType==@(EnumFreightTemplateType.无条件包邮.GetHashCode())">
                                                <tr>
                                                    <th>配送区域</th>
                                                    <th>运费(元)</th>
                                                </tr>
                                                <tr>
                                                    <td><span class="mr-sm label label-primary ng-binding" style="word-wrap:break-word; display:inline-block;">全国统一运费</span></td>
                                                    <td>0</td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>


            <div class="panel-body lml_body lml_body2">
                <div class="lml_row">
                    <span class="lml_tt">运费模板名称</span>
                    <input type="text" class="lml_txt" id="tempname" maxlength="25">
                    <span class="lml_ico lml_red">*</span>
                    <p class="lml_tips">示例：全国包邮、江浙、沪包邮等</p>
                </div>
                <div class="lml_row">
                    <span class="lml_tt">运费政策</span>
                    <div class="yorn">
                        <em class="yradio on" name="lml1"><span class="fa"></span>无条件包邮</em>
                        <em class="yradio" name="lml1"><span class="fa"></span>自定义运费</em>
                    </div>
                    <span class="lml_ico lml_red">*</span>
                </div>
                <div class="radio_content" name="lml1">
                    <div class="lml_content"></div>
                    <div class="lml_row lml_content lmlvaluation">
                        <span class="lml_tt">计价方式</span>
                        <div class="yorn">
                            <em class="yradio on" name="lml2"><span class="fa"></span>按商品重量</em>
                            <em class="yradio" name="lml2"><span class="fa"></span>仅根据区域</em>
                        </div>
                        <span class="lml_ico lml_red">*</span>
                        <div class="lml_row lml_mtop">
                            <span class="lml_tt">运费设置</span>
                            <b class="lml_b">除指定配送区域，其他区域采用<span class="lml_red">全国默认运费</span></b>
                        </div>
                        <div class="radio_content" name="lml2">

                            <div class="lml_content" style="display: block;">
                                <table border="0" cellspacing="1" cellpadding="0" class="lml_tab lml_tab_cont">
                                    <tr>
                                        <th width="25%">配送区域</th>
                                        <th>首重(克)</th>
                                        <th>首费(元)</th>
                                        <th>续重(克)</th>
                                        <th>续费(元)</th>
                                        <th>指定条件包邮(选填)<span class="lml_ttps">对该条件进行设置时，系统<span class="lml_red">优先计算</span>是否包邮<i class="lmli fa"></i></span></th>
                                    </tr>

                                </table>
                                <table border="0" cellspacing="1" cellpadding="0" class="lml_tab lml_tab_bot">
                                    <tr>
                                        <th width="25%"><a href="javascript:void(0)" class="lmla">添加配送区域</a></th>
                                        <th></th>
                                    </tr>
                                </table>
                            </div>
                            <div class="lml_content">
                                <table border="0" cellspacing="1" cellpadding="0" class="lml_tab lml_tab_cont">
                                    <tr>
                                        <th width="50%">配送区域</th>
                                        <th>运费(元)</th>
                                        <th>指定条件包邮(选填)<span class="lml_ttps">对该条件进行设置时，系统<span class="lml_red">优先计算</span>是否包邮<i class="lmli fa"></i></span></th>
                                    </tr>
                                    @*<tr>
                                            <td><span class="lml_red">*</span>全国默认运费</td>
                                            <td><input type="text" class="lml_num" value="3" /></td>
                                            <td>满<input type="text" class="lml_num lml_num1" value="5" />件（含）包邮</td>
                                        </tr>*@
                                </table>
                                <table border="0" cellspacing="1" cellpadding="0" class="lml_tab lml_tab_bot">
                                    <tr>
                                        <th width="50%"><a href="javascript:void(0)" class="lmla">添加配送区域</a></th>
                                        <th></th>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="backbg"></div>
                <div class="bg_cont">
                    <div class="lml_tit w50 lml_line"><em class="ycheckbox" id="checkall"><span class="fa"></span></em>可选配送区域</div>
                    <div class="lml_tit w50"><em class="ycheckbox on" id="checkall1"><span class="fa"></span></em>已选配送区域</div>
                    <ul class="lmlul lmlul1 w50 lml_line"></ul>
                    <ul class="lmlul lmlul2 w50 lml_line"></ul>
                    <div class="lmlclear"></div>
                    <div class="lmlbot">
                        <button type="button" class="lml_button lml_cor3 lml_confirm lmlbutr">确定</button>
                        <button type="button" class="lml_button lml_cor1 lml_cel">取消</button>
                    </div>
                </div>
            </div>
            <div class="panel-footer text-right lml_footer">
                <div>
                    <button type="button" class="btn btn-primary">保存</button>
                    <button type="button" class="btn btn-warning">取消</button>
                </div>
            </div>
        </div>
    </form>
</div>


<script type="text/ng-template" id="passConfirmDialog">
    <div class="ngdialog-message">
        <h3>提示</h3>
        <p>确认删除该模版吗?</p>
        <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
        <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">取消</button>
    </div>
</script>

<script>
    App.controller('FreightManageController', [
    '$scope', '$resource', '$state', '$route', 'ngDialog', '$http', 'ngTableParams', 'ngTableDataService', 'Notify',
    function ($scope, $resource, $state, $route, ngDialog, $http, ngTableParams, ngTableDataService, Notify) {
        'use strict';
        $scope.isLoading = true;
        $scope.tableParams = new ngTableParams({
            page: 1,
            count: 10,
            sorting: {
                Id: 'desc'     // 初始化排序
            }
        }, {
            getData: function ($defer, params) {
                $scope.isLoading = true;
                var requestParams = {
                    Skip: (params.page() - 1) * params.count(),
                    Take: params.count()
                };
                $http.post('/Freight/GetFreightManage', ngTableDataService.getServerSideParams(params.orderBy().toString(), params, requestParams))
                    .success(function (data, status) {
                        params.total(data.Total);
                        $scope.TotalData = data.Total;
                        $defer.resolve(data.Results);
                        $scope.isLoading = false;
                    });
            }
        });

        //删除
        $scope.DelFreight = function (id, isusing) {
            if (!isusing) {
                ngDialog.openConfirm({
                    template: 'passConfirmDialog',
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    $http.post("/Freight/DelFreight", { Id: id }).then(function (returnData) {
                        if (returnData.data.isSuccess) {
                            $scope.tableParams.$params.page = 1;
                            $scope.tableParams.reload();
                            Notify.alert('操作成功!', { status: 'success' });
                        } else {
                            Notify.alert(returnData.data.msg, { status: 'warning' });
                        }
                    });
                }, function (reason) {
                }
                );
            } else {
                Notify.alert("该模版正在使用中，无法删除", { status: 'warning' });
            }
        }




        var template_data = {
            //'TemplateId': 1,
            //'TemplateName': '模版名称',
            //'TemplateType': 1
        };





        var data_source = [
        ];
        //表格颜色
        $('.lml_body1 .lml_tab1 tr:odd').css('background-color', '#fafbfc');

        //区域配送页面

        var lml_body2 = $('.lml_body2'),
            backbg = $('.backbg'),
            bg_cont = $('.bg_cont'),
            lmlul1 = $('.lmlul1', bg_cont),//左侧
            lmlul2 = $('.lmlul2', bg_cont),//右侧
            lmlbot = $('.lmlbot', bg_cont),//底部
            lml_confirm = $('.lml_confirm', lmlbot),//确定按钮
            lml_cel = $('.lml_cel', lmlbot),//取消按钮
            add_btn = $('.lml_tab_bot .lmla'),//新增内容按钮
            yorn = $('.lmlvaluation .yorn'),//计价选项
            //临时可变数据
            lml_content,
            lml_tab_cont;//新增内容
        var region = [];
        $http.post("/CommonArea/GetAreaList", { parentId: 0 }).then(function (returnData) {
            //console.log(returnData.data);
            region = returnData.data;
        });
        var 
        config = { //选中数组
            chekd_arr1: [],
            chekd_arr2: [],
        },
        //临时可变数据
        temp_arr = [],//临时存放的数组
        chekd_arr = [],
        newhtml = '',
        yorn_num,
        html, datasku;
        //判断数据有无


        //取消按钮
        lml_cel.click(function () {
            arehide();
        });
        //添加配送区域按钮
        add_btn.click(function () {
            datasku = undefined;
            areshow();
        });
        //新增区域按钮
        $('.lml_tab_cont').on('click', '.lml_areadd', function () {
            datasku = $(this).parent().parent().data('sku');
            areshow();
        });

        //确定按钮
        lml_confirm.click(function () {
            //return false;
            var arr = [], arr1 = [], len, txt;
            yorn_num = $('.yradio.on', yorn).index();
            chekd_arr = config["chekd_arr" + (yorn_num + 1)];
            len = chekd_arr.length;

            //console.log(datasku,len);
            if (datasku == len) {
                newhtml = '<tr data-sku="' + datasku + '"><td class="lml_tdl">';
            } else {
                newhtml = '<td class="lml_tdl">';
            };


            //console.log(datasku,$('.ycheckbox.on[data-sku='+datasku+']',lmlul2));
            $('.ycheckbox.on[data-sku =' + datasku + ']', lmlul2).each(function () {
                txt = $(this).text();
                newhtml += '<span class="lml_dox">' + txt + '<i class="fa lml_cha"></i></span>';
                arr.push(txt);
            });
            $('.ycheckbox.on', lmlul1).each(function () {
                txt = $(this).text();
                newhtml += '<span class="lml_dox">' + txt + '<i class="fa lml_cha"></i></span>';
                if (datasku == len) {
                    arr1.push(txt);
                } else {
                    arr.push(txt);
                };

            });
            if (datasku == len) {
                //数据后
                switch (yorn_num) {
                    case 1:
                        newhtml += '<span class="lml_areadd fa"></span></td>' +
                            '<td><input type="text" class="lml_num" value="0"/></td>' +
                            '<td>满<input type="text" class="lml_num lml_num1" value="0"/>元（含）包邮<span class="lml_del fa"></span></td>' +
                            '</tr>';
                        break;
                    default:
                        newhtml += '<span class="lml_areadd fa"></span></td>' +
                            '<td><input type="text" class="lml_num" value="0"/></td>' +
                            '<td><input type="text" class="lml_num" value="0"/></td>' +
                            '<td><input type="text" class="lml_num" value="0"/></td>' +
                            '<td><input type="text" class="lml_num" value="0"/></td>' +
                            '<td>满<input type="text" class="lml_num lml_num1" value="0"/>克（含）包邮<span class="lml_del fa"></span></td>' +
                            '</tr>';
                };
            } else {
                newhtml += '<span class="lml_areadd fa"></span></td>';
            };

            if (datasku == len) {
                chekd_arr.push(arr1);
                //console.log(chekd_arr);
                lml_tab_cont.append(newhtml);//添加行区域
            }
            else {
                chekd_arr[datasku] = arr;
                //console.log(newhtml);
                lml_tab_cont.find('tr[data-sku ="' + datasku + '"] .lml_tdl').replaceWith(newhtml);//添加区域
            };
            //console.log(chekd_arr);
            config["chekd_arr" + (yorn_num + 1)] = chekd_arr;
            arehide();
        });
        //单个区域删除
        $('.lml_tab_cont').on('click', '.lml_cha', function () {
            var lml_dox = $(this).parent(), txt, arr,
                datasku = $(this).parent().parent().parent().data("sku");
            txt = lml_dox.text();
            yorn_num = $('.yradio.on', yorn).index();
            arr = config["chekd_arr" + (yorn_num + 1)][datasku];
            arr.splice($.inArray(txt, arr), 1);
            //console.log(datasku, yorn_num, arr, config["chekd_arr" + (yorn_num + 1)], config, $.inArray(txt, arr), txt);
            config["chekd_arr" + (yorn_num + 1)][datasku] = arr;
            //console.log(config["chekd_arr" + (yorn_num + 1)][datasku - 1]);
            lml_dox.remove();
        });
        //行区域删除
        $('.lml_tab_cont').on('click', '.lml_del', function () {
            var tr = $(this).parent().parent(), arr,
                datasku = tr.data('sku'),
                detailId = tr.data('detailid');
            //console.log(detailId);
            if (detailId > 0) {
                $http.post("/Freight/DelFreightDetail", { DetailId: detailId }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        Notify.alert('操作成功!', { status: 'success' });
                        yorn_num = $('.yradio.on', yorn).index();
                        arr = config["chekd_arr" + (yorn_num + 1)][datasku];
                        config["chekd_arr" + (yorn_num + 1)].splice(datasku, 1);
                        tr.remove();
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning' });
                    }
                });
            } else {
                yorn_num = $('.yradio.on', yorn).index();
                arr = config["chekd_arr" + (yorn_num + 1)][datasku];
                config["chekd_arr" + (yorn_num + 1)].splice(datasku, 1);
                tr.remove();
            }
        });
        //
        $('.lml_ttps .lmli').click(function () {
            $(this).parent().hide();
        });
        function arehide() {
            backbg.hide();
            bg_cont.hide();
        };
        function areshow() {
            var len, txt,
                arr = [], arrlen,
                temptxt;
            html = '';
            yorn_num = $('.yradio.on', yorn).index();
            lml_content = $('.radio_content[name=lml2] .lml_content:eq(' + yorn_num + ')');
            lml_tab_cont = $('.lml_tab_cont', lml_content);
            chekd_arr = config["chekd_arr" + (yorn_num + 1)];
            len = chekd_arr.length;

            datasku = (datasku === undefined) ? (len) : datasku;
            //console.log(datasku,len);
            //temptxt = chekd_arr.join()+",";
            temp_arr = [];
            if (len != 0 && datasku < len) {
                //console.log(chekd_arr[datasku]);
                $.each(chekd_arr[datasku], function (i, n) {
                    html += '<li class="lmlli"><em class="ycheckbox on" data-sku="' + datasku + '"><span class="fa"></span>' + n + '</em></li>';
                });

                //console.log(temp_arr,chekd_arr,len);
            };
            //return false;

            //选中
            temptxt = chekd_arr.join() + ",";

            lmlul2.html(html);
            html = '';
            $.each(region, function (i, n) {
                if (temptxt.indexOf(n.Name + ',') == -1) {
                    html += '<li class="lmlli"><em class="ycheckbox"><span class="fa"></span>' + n.Name + '</em></li>';
                };
            });
            lmlul1.html(html);
            backbg.show();
            bg_cont.show();
        };
        //单选按钮
        $('.lml_row .yradio').on('click', function (event) {
            var $this = $(this),
                name, index, lml_content;
            if (!$this.hasClass('disabled')) {
                name = $this.attr('name'),
                index = $this.index(),
                lml_content = $('.radio_content[name=' + name + ']');
                //console.log(index);
                lml_content.find('.lml_content').eq(index).show().siblings('.lml_content').hide();
                $this.addClass('on').siblings('.yradio').removeClass('on');
            };
        });
        // 复选按钮
        $('.bg_cont').on('click', '.ycheckbox', function () {
            var $this = $(this);
            if ($this.hasClass('on')) $this.removeClass('on');
            else $this.addClass('on');
        });
        //保存按钮
        $('.btn-primary.btn').on('click', function () {
            var datas = [], temptxt, template_datas = {};
            yorn_num = $('.yradio.on', yorn).index();
            chekd_arr = config["chekd_arr" + (yorn_num + 1)];
            template_datas.TemplateId = template_data.TemplateId;
            template_datas.TemplateName = $('#tempname').val();

            var type1 = $('.lml_row .yradio.on[name="lml1"]', lml_body2).index();
            if (type1 != 0) {
                type1 = $('.lml_row .yradio.on[name="lml2"]', lml_body2).index() + 1;
            };
            template_datas.TemplateType = type1;
            //console.log(template_datas);

            //console.log(chekd_arr);
            lml_tab_cont = $('.lml_tab_cont:eq(' + yorn_num + ')', lml_body2);
            $('tr:not(:eq(0))', lml_tab_cont).each(function (index, element) {
                var $this = $(this), obj = {}, arr = [];
                obj.detailId = $this.data("detailid");
                if (index != 0) {
                    //console.log('asd', chekd_arr, chekd_arr[index], index, element);
                    temptxt = chekd_arr[index - 1].join() + ",";
                    //return false;
                };
                $('td', $this).each(function (idx, el) {
                    //区域
                    if (idx == 0) {
                        //console.log(obj)
                        if (index == 0) {
                            obj.areas = [-1];
                        } else {
                            $.each(region, function (i, n) {
                                if (temptxt.indexOf(n.Name + ',') != -1) {
                                    arr.push(n.Id);
                                };
                            });
                            //console.log(arr);
                            obj.areas = arr;
                        }
                    } else {

                        obj["data" + (idx)] = $('.lml_num', el).val();
                    };
                });
                //console.log(obj)
                datas.push(obj);

            });
            //console.log(datas);
            //return false;
            var submitflag = true;
            if (template_datas.TemplateName.length == 0) {
                Notify.alert("请填写模版名称", { status: 'warning' });
                submitflag = false;
            }
            if (template_datas.TemplateType == 1) {
                $.each(datas, function (i, n) {
                    //console.log(n.areas);
                    if (n.areas.length == 0) {
                        Notify.alert("请选择区域", { status: 'warning' });
                        submitflag = false;
                        return;
                    }
                    if (n.data1 == 0) {
                        Notify.alert("请填写首重", { status: 'warning' });
                        submitflag = false;
                        return;
                    }
                    if (n.data2 == 0) {
                        Notify.alert("请填写首费", { status: 'warning' });
                        submitflag = false;
                        return;
                    }
                    if (n.data3 == 0) {
                        Notify.alert("请填写续重", { status: 'warning' });
                        submitflag = false;
                        return;
                    }
                    if (n.data4 == 0) {
                        Notify.alert("请填写续费", { status: 'warning' });
                        submitflag = false;
                        return;
                    }
                    if (n.data5 < 0) {
                        Notify.alert("指定包邮条件不能小于0", { status: 'warning' });
                        submitflag = false;
                        return;
                    }
                });
            }
            if (template_datas.TemplateType == 2) {
                $.each(datas, function (i, n) {
                    if (n.areas.length == 0) {
                        Notify.alert("请选择区域", { status: 'warning' });
                        submitflag = false;
                        return;
                    }
                    //if (n.data1 == 0) {
                    //    Notify.alert("请填写运费", { status: 'warning' });
                    //    submitflag = false;
                    //    return;
                    //}
                    if (n.data2 < 0) {
                        Notify.alert("指定包邮条件不能小于0", { status: 'warning' });
                        submitflag = false;
                        return;
                    }
                });
            }

            if (submitflag) {
                $http.post("/Freight/EditFreightSubmit", { Freight: template_datas, FreightDetail: datas }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        Notify.alert('操作成功!', { status: 'success' });
                        $('.lml_body1').show();
                        $('.lml_body2,.lml_footer').hide();
                        template_data = {};
                        data_source = [];
                        $('tr[data-detailid],tr[data-sku]', lml_body2).remove();
                        $('#tempname').val('');
                        $('.lml_row .yradio[name="lml1"]:eq(0)', lml_body2).trigger('click');
                        $('.lml_row .yradio[name="lml2"]:eq(0)', lml_body2).trigger('click');
                        config = {
                            chekd_arr1: [],
                            chekd_arr2: [],
                        };
                        $scope.tableParams.$params.page = 1;
                        $scope.tableParams.reload();
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning' });
                    }
                });
            }

        });

        $("#checkall").click(function () {
            if ($(this).hasClass("on")) {
                $(".lmlul1 .ycheckbox").removeClass("on");
            }
            else {
                

                $(".lmlul1 .ycheckbox").removeClass("on");
                $(".lmlul1 .ycheckbox").addClass("on");
            }
        });

        $("#checkall1").click(function () {
            if ($(this).hasClass("on")) {
                $(".lmlul2 .ycheckbox").removeClass("on");
            }
            else {


                $(".lmlul2 .ycheckbox").removeClass("on");
                $(".lmlul2 .ycheckbox").addClass("on");
            }
        });

        //取消按钮
        $('.btn-warning.btn').click(function () {
            $('.lml_body1').show();
            $('.lml_body2,.lml_footer').hide();
            template_data = {};
            data_source = [];
            $('tr[data-detailid],tr[data-sku]', lml_body2).remove();
            $('#tempname').val('');
            $('.lml_row .yradio[name="lml1"]:eq(0)', lml_body2).trigger('click');
            $('.lml_row .yradio[name="lml2"]:eq(0)', lml_body2).trigger('click');
            config = {
                chekd_arr1: [],
                chekd_arr2: [],
            };
        });

        //新增按钮
        $('.lml_body1 .add_btn').click(function () {

            $http.post("/Freight/EditFreight", { Id: 0 }).then(function (returnData) {

                template_data = returnData.data;
                data_source = returnData.data.DetailResponse;
                $('.lml_body1').hide();
                $('.lml_body2,.lml_footer').show();
                //console.log(data_source);
                $('.lml_tab_cont', lml_body2).each(function (index, el) {
                    newhtml = '';
                    newhtml += '<tr data-detailId="' + data_source[0].detailId + '"><td><span class="lml_red">*</span>' + data_source[0].areas[0].Name + '</td>';
                    var len = (index == 0) ? 6 : 3, unit = (index == 0) ? '克' : '元';
                    for (var i = 1; i < len; i++) {
                        if (i == len - 1) {
                            if (index == 0) {
                                newhtml += '<td>重量<input type="text" class="lml_num lml_num1" value="' + data_source[0]["data" + i] + '" />克（含）以内包邮';
                            } else {
                                newhtml += '<td>满<input type="text" class="lml_num lml_num1" value="' + data_source[0]["data" + i] + '" />元（含）包邮';
                            };
                        } else {
                            newhtml += '<td><input type="text" class="lml_num" value="' + data_source[0]["data" + i] + '"/></td>';
                        };
                    };
                    $(this).append(newhtml);//添加行区域
                });

            });

        });
        //编辑按钮
        $('.table-striped').on('click', '.lml_ico .ico_edit', function () {

            $http.post("/Freight/EditFreight", { Id: $(this).attr("accesskey") }).then(function (returnData) {
                var html1 = "", len1, unit1, ind1;
                template_data = returnData.data;
                data_source = returnData.data.DetailResponse;

                //console.log(data_source);
                $('.lml_body1').hide();
                $('.lml_body2,.lml_footer').show();
                $('#tempname').val(template_data.TemplateName);
                //console.log(template_data.TemplateType);
                if (template_data.TemplateType == 0) {
                    $('.lml_tab_cont', lml_body2).each(function (index, el) {
                        html1 = '';
                        html1 += '<tr data-detailId="0"><td><span class="lml_red">*</span>全国默认运费</td>';
                        len1 = (index == 0) ? 6 : 3, unit1 = (index == 0) ? '克' : '元';
                        for (var i = 1; i < len1; i++) {
                            if (i == len1 - 1) {
                                if (index == 0) {
                                    html1 += '<td>重量<input type="text" class="lml_num lml_num1" value="0" />克（含）以内包邮';
                                } else {
                                    html1 += '<td>满<input type="text" class="lml_num lml_num1" value="0" />元（含）包邮';
                                };
                            } else {
                                html1 += '<td><input type="text" class="lml_num" value="0"/></td>';
                            };
                        };
                        $(this).append(html1);//添加行区域
                    });
                };

                //运费政策判断
                if (template_data.TemplateType > 0) {
                    $('.lml_row .yradio[name="lml1"]:eq(1)', lml_body2).trigger('click');
                    //计价方式判断
                    var types = template_data.TemplateType, len = (types == 1) ? 6 : 3, unit = (types == 1) ? "克" : "元";
                    if (types != 0) {
                        len1 = (types == 1) ? 2 : 5,
                        unit1 = (types == 1) ? "克" : "元";
                        ind1 = (types == 1) ? 1 : 0;
                        $('.lml_row .yradio[name="lml2"]:eq(' + (types - 1) + ')', lml_body2).trigger('click');
                        //表格内容
                        html1 += '<tr data-detailId="0"><td><span class="lml_red">*</span>全国默认运费</td>';
                        for (var i = 0; i < len1; i++) {
                            if (i == (len1 - 1)) {
                                if (types == 2) {
                                    html1 += '<td>重量<input type="text" class="lml_num lml_num1" value="0" />克（含）以内包邮';
                                } else {
                                    html1 += '<td>满<input type="text" class="lml_num lml_num1" value="0" />元（含）包邮';
                                };
                            } else {
                                html1 += '<td><input type="text" class="lml_num" value="0"/></td>';
                            };
                        };
                    };

                    $('.lml_tab_cont:eq(' + ind1 + ')', lml_body2).append(html1);
                    newhtml = "";
                    $.each(data_source, function (i, n) {
                        var arr = [];
                        for (var j = 0; j < len; j++) {
                            if (j == 0) {
                                if (n.areas[0].Id == -1) {
                                    newhtml += '<tr data-detailId="' + n.detailId + '"><td><span class="lml_red">*</span>' + n.areas[0].Name + '</td>';
                                } else {
                                    newhtml += '<tr data-sku="' + (i - 1) + '" data-detailId="' + n.detailId + '"><td class="lml_tdl">';
                                    datasku = i;
                                    $.each(n.areas, function (inx, el) {
                                        arr.push(el.Name);
                                        newhtml += '<span class="lml_dox">' + el.Name + '<i class="fa lml_cha"></i></span>';
                                    });
                                    newhtml += '<span class="lml_areadd fa"></span></td>';
                                };
                                //kkkk
                            } else if (j == (len - 1)) {
                                if (types == 1) {
                                    newhtml += '<td>重量<input type="text" class="lml_num lml_num1" value="' + n["data" + j] + '"/>克（含）以内包邮';
                                } else if (types == 2) {
                                    newhtml += '<td>满<input type="text" class="lml_num lml_num1" value="' + n["data" + j] + '" />元（含）包邮';
                                };
                                //newhtml += '<td>满<input type="text" class="lml_num lml_num1" value="' + n["data" + j] + '" />' + unit + '（含）包邮';
                                if (n.areas[0].Id != -1) {
                                    newhtml += '<span class="lml_del fa"></span>';
                                };
                                newhtml += '</td></tr>';
                            } else {
                                newhtml += '<td><input type="text" class="lml_num" value="' + n["data" + j] + '"/></td>';
                            };
                        };
                        if (n.areas[0].Id != -1) config["chekd_arr" + (types)].push(arr);

                    });
                    lml_tab_cont = $('.lml_tab_cont:eq(' + (types - 1) + ')', lml_body2);
                    lml_tab_cont.append(newhtml);//添加行区域
                    //console.log(config["chekd_arr"+(types+1)]);
                };
            });
        });

    }]);
</script>
